﻿
Imports CaptchaDLL
Imports MeoTom.Core

Partial Public Class Pages
    Inherits UI.Page

    Private ReadOnly _objPage As New BLL.Pages()
    Private ReadOnly _util As New Utilities.Common()

    Public PageTitle As String = ""
    Public PageAbstract As String = ""
    Public PageUrl As String = ""
    Private PageID As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _util.SetSession("CaptchaImageText", CaptchaImage.GenerateRandomCode(CaptchaType.AlphaNumeric, 4))
        GetArticle()
    End Sub

    Public Sub GetArticle()
        Try
            PageID = Request.QueryString("PageID")
            PageUrl = "/Pages/" & PageID & "/" & Request.QueryString("sPath")
            Dim dYear As String = Left(PageID, 4)
            Dim dMonth As String = Integer.Parse(PageID.Substring(4, 2)).ToString()
            Dim dDay As String = PageID.Substring(6, 2)

            'read content from xml
            _objPage.ReadXmlById(dYear, dMonth, dDay, PageID)

            'write comment
            CommentContent.InnerHtml = WriteComment(PageID)

            If Not PageUrl = _objPage.PagePath Then
                'lg.WriteLog("Redirect 1", "PageID:" & PageID & "|strUrl=" & strUrl & "|dYear:" & dYear & "|dMonth:" & dMonth & "|dDay:" & dDay & "|Url:____" & objPage.PagePath)
                Response.Redirect("/Error?e=404", False)
            Else
                If _objPage.Status Then
                    If Not _objPage.ReleaseDate > DateTime.Now Then
                        If _objPage.PostVisible = "1" Then
                            If _util.GetCookie("UserName") <> vbNullString Or _util.GetSession("UserName") <> vbNullString Then
                                LoadArticle()
                            Else
                                If _objPage.PagePassword = vbNullString Then
                                    LoadArticle()
                                Else
                                    If _util.GetSession("Pwd_" & _objPage.PageID) = _objPage.PageID Then
                                        LoadArticle()
                                    Else
                                        ArticleContent.Visible = False
                                        ArticlePassword.Visible = True
                                        CommentContent.Visible = False
                                        CommentForm.Visible = False
                                        Page.Title = "MeoTom's Blog: Enter Password."
                                    End If
                                End If
                            End If
                        ElseIf _objPage.PostVisible = "2" Then
                            If _util.GetCookie("UserName") <> vbNullString Or _util.GetSession("UserName") <> vbNullString Then
                                LoadArticle()
                            Else
                                Response.Redirect("/Error?e=404", False)
                            End If
                        Else
                            LoadArticle()
                        End If
                    Else
                        Response.Redirect("/Error?e=404", False)
                    End If
                Else
                    Response.Redirect("/Error?e=404", False)
                End If
            End If
        Catch ex As Exception
        End Try
    End Sub

    Public Sub LoadArticle()
        _objPage.UpdateViewCount()

        lnkTitle.Text = _objPage.PageTitle
        ltrAbstract.Text = Server.HtmlDecode(_objPage.Body.ToString)
        lnkTitle.NavigateUrl = PageUrl

        If _objPage.CommentsEnabled = False Then
            CommentForm.Visible = False
        Else
            ltrAbstract.Text &= "<script language='javascript' type='text/javascript'>Util.writeEmoticons('divEmoticons');</script>"
        End If

        PageTitle = _objPage.PageTitle.ToString
        PageAbstract = Left(_util.NoHTML(Server.HtmlDecode(Server.HtmlDecode(_objPage.Body.ToString.Replace(System.Environment.NewLine, " ")))), 200)

        ltrAbstract.Text &= "<script language='javascript' type='text/javascript'>"
        ltrAbstract.Text &= "   window.onload = function() {"
        ltrAbstract.Text &= "       Util.sociable('" & Configs.HomeUrl & PageUrl & "', '" & PageTitle.Replace(" ", "+").Replace("'", "") & "', '" & Left(PageAbstract.Replace(" ", "+"), 50).Replace("'", "") & "', 'sociable');"
        ltrAbstract.Text &= "   }"
        ltrAbstract.Text &= "</script>"

        Page.Title = Server.HtmlDecode(PageTitle) & " - " & Configs.WebTitle
        Dim metaTag As New HtmlMeta()
        metaTag.Name = "description"
        metaTag.Content = Server.HtmlDecode(PageTitle) & " - " & PageAbstract
        Header.Controls.Add(metaTag)
    End Sub

    Public Function WriteComment(ByVal PageID As String) As String
        Dim TotalComment As Integer = 1
        Dim strOut As New StringBuilder()
        Dim strComment As New StringBuilder()
        Using dc As New MeoTomDataContext
            Dim CommentSource = From C In dc.Comments Where C.PageID = PageID And C.Status = 0 Order By C.CommentsID Ascending Select C.CommentsID, C.AddedBy, C.AddedByEmail, C.AddedByIP, C.AddedDate, C.Body, C.Status, C.AddedByURL, C.IsAdmin
            For Each CommentItem In CommentSource
                If CommentItem.IsAdmin Then
                    strComment.Append("<li id=""Comments_").Append(TotalComment).Append(""" class=""AdminComment"">")
                Else
                    strComment.Append("<li id=""Comments_").Append(TotalComment).Append(""">")
                End If

                If CommentItem.AddedByURL = vbNullString Then
                    strComment.Append("<cite>").Append(CommentItem.AddedBy).Append(":</cite><br />")
                Else
                    strComment.Append("<cite><a href=""").Append(CommentItem.AddedByURL).Append(""" target=""blank"">").Append(CommentItem.AddedBy).Append(":</a></cite><br />")
                End If

                strComment.Append("<small class=""commentmetadata""><a href=""#Comments_").Append(TotalComment).Append(""">").Append(String.Format("{0:dd/MM/yyyy}", CommentItem.AddedDate)).Append(" at ").Append(String.Format("{0:HH:mm tt}", CommentItem.AddedDate)).Append("</a></small>")
                strComment.Append("<p>").Append(CommentItem.Body).Append("</p>")

                If CommentItem.IsAdmin Then
                    strComment.Append("<span class=""number NumberAdmin"">").Append(TotalComment).Append("</span>")
                Else
                    strComment.Append("<span class=""number"">").Append(TotalComment).Append("</span>")
                End If

                strComment.Append("</li>")
                TotalComment = TotalComment + 1
            Next
        End Using

        If Not strComment.Length = 0 Then
            strOut.Append("<h3><a href=""#Comments"" name=""Comments"" id=""Comments"" title=""Bình luận"">Bình luận (").Append(TotalComment - 1).Append(")</a></h3><div class=""hr fl"">&nbsp;</div>")
            strOut.Append("<ol class=""CommentList"" id=""CommentList"">")
            strOut.Append(strComment.ToString)
            strOut.Append("</ol>")
        End If
        Return strOut.ToString
    End Function
End Class